package cn.ronghuanet.org.controller;

import cn.ronghuanet.basic.annotation.RonghuaPermission;
import cn.ronghuanet.basic.util.AjaxResult;
import cn.ronghuanet.basic.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理Controller",description = "部门的增删查改接口")
@RonghuaPermission(name = "部门管理",desc = "管理部门的接口")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    // 增 insert // 改 update 使用put请求控制
    @ApiOperation(value = "新增或修改部门")
    @PutMapping // put请求 还是 post请求 这里要写@RequestBody
    @RonghuaPermission(name = "部门新增和修改",desc = "部门新增和修改")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (Objects.isNull(department.getId())){
                departmentService.insert(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    // 删 delete
    @RonghuaPermission(name = "删除指定部门",desc = "删除指定部门")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    // 查一级和二级部门
    @RonghuaPermission(name = "查询部门树",desc = "查询部门树")
    @GetMapping("/tree")
    public AjaxResult getTree(){
        try {
            List<Department> departments = departmentService.getTree();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    // 查 queryById  get
    @RonghuaPermission(name = "查询指定部门",desc = "查询指定部门")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id){
        try {
            Department department = departmentService.queryById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    // 查 queryAll  get
    @RonghuaPermission(name = "查询所有部门",desc = "查询所有部门")
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.queryAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RonghuaPermission(name = "部门分页查询",desc = "部门分页查询")
    @PostMapping
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> page = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RonghuaPermission(name = "部门批量删除",desc = "部门批量删除")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody  List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
